As
you have seen in this article, it is fairly easy to create and
implement SSIS packages to do data transformations from one or more
data sources to one or more data destinations. However, many
organizations still really just need a vanilla and very fast mechanism
to export data out of SQL Server or import data into SQL Server. bcp fills this need well (and has done so from the beginning of SQL Server).
The following sections outline the primary execution methods of bcp, the many switches of bcp, the format file, and ways to improve performance when using bcp. By the end of these sections, you will be able to optimally execute bcp
successfully for several common production scenarios. Microsoft has
added a new execution switch that generates an XML format file. Most
other features of bcp have remained the same.
First, you need to see whether you have the right version of bcp. A quick check of your version of bcp guarantees that you won’t run into any limitations from older versions of bcp that might be left on your servers. You can do this by executing bcp at the command prompt with the –v option and no other parameters. (Note that all bcp switch options are case sensitive; for example, –v and –V are two very different switches.) Here’s an example:
C:> bcp –v
BCP - Bulk Copy Program for Microsoft SQL Server.
Copyright (C) Microsoft Corporation. All Rights Reserved.
Version: 10.0.1600.22
This is version 10.0,
which is distributed with MS SQL Server 2008 (SQL Server 10.0). If a
version other than 10.x is present here, you must re-install bcp immediately.
At any time, you can see the proper usage and bcp switch options available by executing bcp at the command prompt with a question mark (?):
C:> bcp ?
usage: bcp {dbtable | query} {in | out | queryout | format} datafile
[-m maxerrors] [-f formatfile] [-e errfile]
. . .
You use the following syntax for bcp, along with one or more switches:
bcp {dbtable | query} {in | out | queryout | format} datafile
In this syntax, dbtable is the database_name, schema, and table_name | view_name (for example, AdventureWorks2008.Production.Product or "AdventureWorks2008.Production.Product"):
database_name—
This is the name of the database in which the specified table or view
resides. If not specified, this is the default database for the user.
owner— This is the name of the schema of the table or view.
table_name | view_name— This is the name of the destination table or view when copying data into SQL Server (in), and it is the name of the source table when copying data from SQL Server (out).
query is a T-SQL query that returns a result set. queryout must also be specified when bulk-copying data from a query.
in | out | queryout | format specifies the direction of the bulk copy (in copies from a file in to the database table or view, out copies from the database table or view to a file). queryout must be specified when bulk-copying data from a query. format creates a format file based on the switch specified (–n, –c, –w, –V, or –N) and the table or view delimiters. If format is used, the –f option must be specified as well.
data_file is the data file used when bulk-copying a table or view into or out of SQL Server.
All the available bcp switches are listed in Table 1.
Table 1. bcp Switches
Switch | Description | Example |
---|
-m | Specifies the maximum number of errors to allow before stopping the transfer. The default is 10. | [–m max_errors] |
-f | Specifies the format file used to customize the load or unload data in a specific style. | [-f format_file] |
-e | Specifies the file to write error messages to. | [-e err_file] |
-F | Specifies the first row in the data file to start copying from when importing. The default is 1. | [-F first_row] |
-L | Specifies the last row in the data file to end copying with when importing. The default is 0, which indicates the last row in the file. | [-L last_row] |
-b | Specifies the number of rows to include in each committed batch. By default, all data rows in a file are copied in one batch. | [-b batch_size] |
-n | Specifies that native (database) data type formats are to be used for the data. | [-n] |
-c | Specifies that character data type format is to be used for the data. In addition, \t (tab character) is used as the field separator, and \n (newline character) is used as the row terminator. | [-c] |
-w | Specifies that the Unicode data type format is to be used for the data. In addition, \t (tab character) is used as the field separator, and \n (newline character) is used as the row terminator. | [-w] |
-N | Specifies to use Unicode for character data and native format for all others. This can be used as an alternative to the –w switch. | [-N] |
-V | Specifies to use data type formats from earlier versions of SQL Server. | [-V (70 | 80|90)] |
-q | Tells bcp to use quoted identifiers when dealing with table and column names. | [-q] |
-C | If you are loading extended characters, allows you to specify the code page of the data in the data file. | [-C code_page] |
-t | Specifies the terminating character(s) for fields. The default is \t (tab character). | [-t field_term] |
-r | Specifies the terminating character(s) for rows. The default is \n (newline character). | [-r row_term] |
-i | Specifies a file for redirecting input into bcp (the response file containing the responses to the command prompts). | [-i input_file] |
-o | Specifies the file for receiving redirected output from bcp. | [-o output_file] |
-a | Specifies
the network packet size (in bytes) used to send to or receive from SQL
Server. Can be between 4,096 and 65,535 bytes. The default size is
4,096. | [-a packet_size] |
-S | Specifies the SQL Server name to connect to. Local is the default. | [-S server_name | server_name\instance_name] |
-U | Specifies
the user account to log in as; this account must have sufficient
privileges to carry out either a read or a write of the table. | [-U login_id] |
-P | Specifies the password associated with the user account. | [-P password] |
-T | Makes a trusted connection to the server, using the network user/security credentials instead of the login_id/password. | [-T] |
-v | Displays the bcp version information. | [-v] |
-R | Uses the regional format for currency, date, and time data, as defined by the locale settings of the client computer. | [-R] |
-k | Overrides a column’s default and enforces NULL values being loaded into the columns as part of the bcp operation. | [-k] |
-E | Uses the identity values in the import file rather than generating new ones. | [-E] |
-h | Specifies special hints to be used during the bcp
operation. They include the following: the sort order of the data file,
number of rows of data per batch, number of kilobytes of data per
batch, whether to acquire a table-level lock, whether to check
constraints, and whether to fire insert triggers. | [-h hint_type,..] |
-x | Generates an XML format file. | [-x] |
.